package com.jcommander.gui.table;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import com.jcommander.model.FileSystemRow;

public class FileSystemTableModel extends AbstractTableModel{
	
	private static final long serialVersionUID = 7990082338103245378L;

	private List<String> columnNames = new ArrayList<String>();
    
    private List<List<Object>> data = new ArrayList<List<Object>>();
    
    public FileSystemTableModel() {
    	columnNames.add("ID");
    	columnNames.add("Name");
    	columnNames.add("Ext");
    	columnNames.add("Size");
    	columnNames.add("Date");
    }
    
    public void addData(List<FileSystemRow> fileSystemRows) {
    	for (FileSystemRow row : fileSystemRows) {
    		List<Object> dataRow = new ArrayList<Object>();
    		dataRow.add(row.getId());
    		dataRow.add(row.getIconLabel());
    		dataRow.add(row.getExtension());
    		dataRow.add(row.getSize());
    		dataRow.add(row.getDate());
    		
    		data.add(dataRow);
    	}
    }
    
    @Override
    public String getColumnName(int col) {
        return columnNames.get(col);
    }
    
	public int getColumnCount() {
		return columnNames.size();
	}

	public int getRowCount() {
		return data.size();
	}

    public Object getValueAt(int rowIndex, int columnIndex) {
        if (rowIndex < data.size()) {
            if (columnIndex < data.get(rowIndex).size()) {
                return data.get(rowIndex).get(columnIndex);
            }
        }
        return null; 
    }
    
    @Override
    public Class<?> getColumnClass(int c) {
    	Object value = this.getValueAt(0, c);  
    	
    	return (value == null ? Object.class : value.getClass()); 
    }
    
    @Override
    public boolean isCellEditable(int row, int col) {
        return false;
    }
    
    @Override
    public void setValueAt(Object value, int rowIndex, int colIndex) {
        List<Object> row = data.get(rowIndex);
        row.set(colIndex, value);
        fireTableCellUpdated(rowIndex, colIndex);

    }
    
    public void clearData() {
        data.clear();
    }

}
